﻿<%@ WebHandler Language="C#" Class="OfferPriceHandler" %>

using System;
using System.Web;
using System.Collections;
using System.Data;
using StarTech.DBUtility;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Web.DynamicData;


public class OfferPriceHandler : IHttpHandler
{
    AdoHelper ado = AdoHelper.CreateHelper(StarTech.Util.AppConfig.DBInstance);
    string memberId = "";
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        if (context.Request.Cookies["memberInfo"] != null && Common.CheckUser(context.Request.Cookies["memberInfo"].Value))
        if (true)
        {
            memberId = context.Request.Cookies["memberInfo"].Value.Split(new string[] { "$$" }, StringSplitOptions.RemoveEmptyEntries)[0];
            
            string flag = Common.NullToEmpty(context.Request["flag"]);
            string orderId = Common.NullToEmpty(context.Request["orderId"]);

            switch (flag.ToLower())
            {
                case "info":
                    context.Response.Write(GetPayInfo(orderId));
                    break;
                case "pay":
                    context.Response.Write(PayOrder(orderId));
                    break;
                case "paybyye":
                    context.Response.Write(PayOrderByYE(orderId));
                    break;
            }
        }
        else
        {
            context.Response.Write("please login");
        }
    }

    private string GetPayInfo(string orderId)
    {
        SqlParameter[] p = { new SqlParameter("@orderId", orderId) };
        string strSql = @"select e.GoodsName,f.CategoryName,c.JobDay,c.JobStartTime,c.JobEndTime,d.TrueName,c.MemberPrice,e.JobType from T_Order_InfoDetail a
left join T_Order_Info b on a.OrderId=b.OrderId
left join T_Goods_MemberPrice c on b.PriceId=c.PriceId
left join T_Member_Info d on b.MemberId=d.MemberId
left join T_Goods_Info e on a.GoodsId=e.GoodsId
left join T_Info_Category f on e.CategoryId=f.CategoryName
where a.OrderId=@orderId";

        return JSONHelper.GetJSON(ado.ExecuteSqlDataset(strSql, p).Tables[0]);
    }
    
    
    //余额支付
    private string PayOrderByYE(string orderId)
    {
        object objMoney = ado.ExecuteSqlScalar("select OrderAllMoney from T_Order_Info where orderId='" + orderId + "'");
        if (objMoney == null) { return "订单号错误"; }
        decimal orderMoney = decimal.Parse(objMoney.ToString());    //订单金额

        object objYE = ado.ExecuteSqlScalar("select account_money from T_Moneybag_AccountInfo where member_id='" + this.memberId + "'");
        string strYE = objYE == null ? "0" : objYE.ToString();
        decimal yeMoney = decimal.Parse(strYE);                     //账户余额

        if (yeMoney < orderMoney) { return "余额不足"; }
        
        //余额支付
        if (ado.ExecuteSqlScalar("select orderId from T_Order_Info where orderId='" + orderId + "' and IsPay=1") == null)
        {
            PayOrder(orderId);
            MoneyBagTools.CreateMoneyBagDetail(this.memberId, -orderMoney, "xfye", orderId);
        }
        else
        {
            return "订单已支付";
        }
        
        return "1";
    }
    

    private string PayOrder(string orderId)
    {
        SqlParameter[] p = { new SqlParameter("@orderId", orderId) };
        string goodsId = ado.ExecuteSqlScalar("select goodsId from T_Order_InfoDetail where orderId=@orderId", p).ToString();

        SqlParameter[] p1 = { new SqlParameter("@goodsId", goodsId) };
        ado.ExecuteSqlNonQuery("update T_Goods_Info set JobStatus='进行中' where goodsId=@goodsId", p1);


        string mId = Common.GetWorkerIdByOrderId(orderId);
        string GoodsName = Common.GetGoodsNameByOrderId(orderId);

        string jobType = Common.GetJobTypeByOrderId(orderId);
        Common.AddLogMsg(mId, "订单支付", "雇主已支付——【" + GoodsName + "】(" + orderId + ") 的款项", "MyAcceptTask" + (jobType == "DG" ? "People" : "") + "List.html", 0);
        
        
        SqlParameter[] p3 = { new SqlParameter("@orderId", orderId) };
        string strSql3 = "update T_Order_Info set IsPay='1',payTime=getdate() where orderId=@orderId";
        return ado.ExecuteSqlNonQuery(strSql3, p3).ToString();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}